package web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import web.Const;

public class SessionFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// request handling pre-servlet activation
		System.out.println("pre filtering");
		HttpSession session = ((HttpServletRequest) request).getSession(false);

		if (session == null || ((session.getAttribute(Const.ID_USER) == null) || (session.getAttribute(Const.PASSWORD) == null))) {

			((HttpServletResponse) response).sendRedirect("index.jsp");

			return;
		}

		chain.doFilter(request, response);
		// response handling post-servlet activation
		System.out.println("post filtering");

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
